
var goodTypeTree;
var goodTypeTreeRoot;

$(function () {
    var addGoodTypeOptions = {
        message: "请输入类别名"
        , title: '增加商品类别'
        , size: eModal.size.sm
    };

    //可以通过下面的方法来创建一个右键上下文菜单。
    var contex_menu = {
        'context': {
            elements: [
                {
                    text: '增加子类型',
                    icon: 'assets/AimaraTree/img/add.png',
                    action: function (node) {
                        eModal.prompt(addGoodTypeOptions).then(function (value) {
                            $.ajax({
                                url: ctx + '/goodType/add.do',
                                data: {
                                    name: value,
                                    level: node.tag.level + 1,
                                    parentId: node.tag.id,
                                    parentNodeCode: node.tag.code
                                },
                                dataType: 'json',
                                type: 'post',
                                success: function (data) {
                                    var nodeObj = {};
                                    nodeObj.id = data.id;
                                    nodeObj.name = value;
                                    nodeObj.level = node.tag.level + 1;
                                    nodeObj.code = data.code;
                                    if(nodeObj.level!=2)
                                        node.createChildNode(nodeObj.name, false, null, nodeObj, 'context');
                                    else
                                        node.createChildNode(nodeObj.name, false, null, nodeObj, 'leafContext');
                                    node.expandNode();
                                }
                            });
                        }, function () {
                        });
                    }
                },
                {
                    text: '删除',
                    icon: 'assets/AimaraTree/img/delete.png',
                    action: function (node) {
                        var addGoodTypeOptions = {
                            message: "确认删除？"
                            , title: '提示'
                            , size: eModal.size.sm
                        };
                        eModal.confirm(addGoodTypeOptions)
                            .then(function () {
                                $.ajax({
                                    url: ctx + '/goodType/delete.do',
                                    data: {
                                        id: node.tag.id
                                    },
                                    dataType: 'json',
                                    type: 'post',
                                    success: function (data) {
                                        goodTypeTree.removeNode(node);
                                        var alertOptions = {
                                            message: "删除成功"
                                            , title: '提示'
                                            , size: eModal.size.sm
                                        };
                                        setTimeout(function () {
                                            eModal.alert(alertOptions)
                                        }, 500);
                                    }
                                });

                            }, function () {

                            });
                    }
                }]
        },
        'rootContext': {
            elements: [
                {
                    text: '增加子类型',
                    icon: 'assets/AimaraTree/img/add.png',
                    action: function (node) {
                        eModal.prompt(addGoodTypeOptions).then(function (value) {
                            $.ajax({
                                url: ctx + '/goodType/add.do',
                                data: {
                                    name: value,
                                    level: 0
                                },
                                dataType: 'json',
                                type: 'post',
                                success: function (data) {
                                    var alertOptions = {
                                        message: "添加成功"
                                        , title: '提示'
                                        , size: eModal.size.sm
                                    };
                                    setTimeout(function () {
                                        eModal.alert(alertOptions)
                                    }, 500);
                                    goodTypeTreeRoot.expandNode();
                                    var nodeObj = {};
                                    nodeObj.id = data.id;
                                    nodeObj.name = value;
                                    nodeObj.level = 0;
                                    nodeObj.code = data.code;
                                    goodTypeTreeRoot.createChildNode(nodeObj.name, false, null, nodeObj, 'context');
                                }
                            });
                        }, function () {
                        });
                    }
                }]
        },
        'leafContext': {
            elements: [
                {
                    text: '删除',
                    icon: 'assets/AimaraTree/img/delete.png',
                    action: function (node) {
                        var addGoodTypeOptions = {
                            message: "确认删除？"
                            , title: '提示'
                            , size: eModal.size.sm
                        };
                        eModal.confirm(addGoodTypeOptions)
                            .then(function () {
                                $.ajax({
                                    url: ctx + '/goodType/delete.do',
                                    data: {
                                        id: node.tag.id
                                    },
                                    dataType: 'json',
                                    type: 'post',
                                    success: function (data) {
                                        goodTypeTree.removeNode(node);
                                        var alertOptions = {
                                            message: "删除成功"
                                            , title: '提示'
                                            , size: eModal.size.sm
                                        };
                                        setTimeout(function () {
                                            eModal.alert(alertOptions)
                                        }, 500);
                                    }
                                });

                            }, function () {

                            });
                    }
                }]
        }
    };
    //创建树结构
    goodTypeTree = createTree('div_tree', 'rgb(241, 242, 247)', contex_menu, {
        onSelectNode: function (node) {
            $("#propertyContainer").html('');
            $("#propertyValContainer").html('');
            if (node.tag && node.tag.level == 2) {
                $.ajax({
                    url: ctx + '/goodTypeProperty/getAllProperty.do',
                    dataType: 'json',
                    data: {id: node.tag.id},
                    type: 'post',
                    success: function (data) {
                        for(var i=0;i<data.length;i++){
                            $('#propertyContainer').append('<span propertyId="' + data[i].ID + '" class="label label-default propertyName" onclick="changeLabelColor(this)">' + data[i].NAME + '</span>')
                        }
                    }
                });
            }
        }
    });
    //创建树节点node1
    goodTypeTreeRoot = goodTypeTree.createNode('商品类别树', false, null, null,
        null, 'rootContext');
    function createNode(parentNode, nodeObj) {
        if(nodeObj.level==2)
            var newNode = parentNode.createChildNode(nodeObj.name, false, null, nodeObj, 'leafContext');
        else
            var newNode = parentNode.createChildNode(nodeObj.name, false, null, nodeObj, 'context');
        var node;
        for (var i = 0; i < nodeObj.children.length; i++) {
            createNode(newNode, nodeObj.children[i]);
        }
    }

    $.ajax({
        url: ctx + '/goodType/getTypeTree.do',
        dataType: 'json',
        type: 'post',
        success: function (nodes) {
            for (var i = 0; i < nodes.length; i++) {
                createNode(goodTypeTreeRoot, nodes[i]);
            }
            //渲染树结构
            goodTypeTree.drawTree();
            goodTypeTreeRoot.expandNode();
        }
    });


    $("#deleteProperty").click(function () {
        if ($('.propertyName.selected').length == 0) {
            var alertOptions = {
                message: "请选择属性"
                , title: '提示'
                , size: eModal.size.sm
            };
            eModal.alert(alertOptions);
        } else {
            $.ajax({
                url:ctx+'/goodTypeProperty/delete.do',
                data:{
                    id:$('.propertyName.selected').attr('propertyId')
                },
                dataType:'json',
                type:'post',
                success:function(data){
                    $('.propertyName.selected').remove();
                }
            });
        }
    });

    $("#searchProperty").click(function(){
        $("#propertyContainer").html('');

        if (goodTypeTree.selectedNode.tag && goodTypeTree.selectedNode.tag.level == 2) {
            $.ajax({
                url: ctx + '/goodTypeProperty/getAllProperty.do',
                dataType: 'json',
                data: {id: goodTypeTree.selectedNode.tag.id,name:$('#propertyName').val().trim()},
                type: 'post',
                success: function (data) {
                    for(var i=0;i<data.length;i++){
                        $('#propertyContainer').append('<span propertyId="' + data[i].ID + '" class="label label-default propertyName" onclick="changeLabelColor(this)">' + data[i].NAME + '</span>')
                    }
                }
            });
        }else{
            var alertOptions = {
                message: "请选择第三级类型"
                , title: '提示'
                , size: eModal.size.sm
            };
            eModal.alert(alertOptions);
        }
    });
    $("#addProperty").click(function () {
        var propertyName = $('#propertyName').val().trim();
        if (propertyName == "") {
            var alertOptions = {
                message: "请输入属性名"
                , title: '提示'
                , size: eModal.size.sm
            };
            eModal.alert(alertOptions);
        } else {
            if (!goodTypeTree.selectedNode || !goodTypeTree.selectedNode.tag) {
                var alertOptions = {
                    message: "请选择类型"
                    , title: '提示'
                    , size: eModal.size.sm
                };
                eModal.alert(alertOptions);
                return;
            } else if (goodTypeTree.selectedNode.tag.level != 2) {
                var alertOptions = {
                    message: "请选择第三级类型"
                    , title: '提示'
                    , size: eModal.size.sm
                };
                eModal.alert(alertOptions);
                return;
            } else {
                //TODO:判断该属性名是否有重复
                $.ajax({
                    url: ctx + '/goodTypeProperty/add.do',
                    data: {
                        name: propertyName,
                        goodTypeId: goodTypeTree.selectedNode.tag.id
                    },
                    dataType: 'json',
                    type: 'post',
                    success: function (data) {
                        $('#propertyContainer').append('<span propertyId="' + data.id + '" class="label label-default propertyName" onclick="changeLabelColor(this)">' + propertyName + '</span>')
                        $('#propertyName').val('');
                    }
                });
            }
        }
    });

    $("#addPropertyVal").click(function(){
        var propertyVal = $('#propertyVal').val().trim();
        if (propertyVal == "") {
            var alertOptions = {
                message: "请输入属性值"
                , title: '提示'
                , size: eModal.size.sm
            };
            eModal.alert(alertOptions);
        }else if ($('.propertyName.selected').length == 0) {
            var alertOptions = {
                message: "请选择属性"
                , title: '提示'
                , size: eModal.size.sm
            };
            eModal.alert(alertOptions);
        }else{
            $.ajax({
                url:ctx+'/goodTypePropertyValue/add.do',
                data:{
                    name:propertyVal,
                    goodTypePropertyId:$('.propertyName.selected').attr('propertyId')
                },
                dataType:'json',
                type:'post',
                success:function(data){
                    $('#propertyValContainer').append('<span propertyValId="' + data.id + '" class="label label-default propertyVal" onclick="changeValLabelColor(this)">' + propertyVal + '</span>');
                    $('#propertyVal').val('');
                }
            });
        }
    });
    $("#deletePropertyVal").click(function(){
        if ($('.propertyVal.selected').length == 0) {
            var alertOptions = {
                message: "请选择属性值"
                , title: '提示'
                , size: eModal.size.sm
            };
            eModal.alert(alertOptions);
        } else {
            $.ajax({
                url:ctx+'/goodTypePropertyValue/delete.do',
                data:{
                    id:$('.propertyVal.selected').attr('propertyValId')
                },
                dataType:'json',
                type:'post',
                success:function(data){
                    $('.propertyVal.selected').remove();
                }
            });
        }
    });
    $("#searchPropertyVal").click(function(){
        $('#propertyValContainer').html('');
        if ($('.propertyName.selected').length == 0) {
            var alertOptions = {
                message: "请选择属性"
                , title: '提示'
                , size: eModal.size.sm
            };
            eModal.alert(alertOptions);
        }else{
            $.ajax({
                url:ctx+'/goodTypePropertyValue/search.do',
                data:{
                    id:$('.propertyName.selected').attr('propertyId'),
                    name:$('#propertyVal').val().trim()
                },
                dataType:'json',
                type:'post',
                success:function(data){
                    for(var i=0;i<data.length;i++){
                        $('#propertyValContainer').append('<span propertyValId="' + data[i].ID + '" class="label label-default propertyVal" onclick="changeValLabelColor(this)">' + data[i].NAME + '</span>');
                    }
                }
            });
        }
    });
});

var changeValLabelColor = function (target) {
    $('.propertyVal').removeClass('selected');
    $(target).addClass('selected');
}

var changeLabelColor = function (target) {
    $('#propertyValContainer').html('')
    $('#propertyVal').val('')
    $('.propertyName').removeClass('selected');
    $(target).addClass('selected');
    $.ajax({
        url:ctx+'/goodTypePropertyValue/search.do',
        data:{
            id:$(target).attr('propertyId')
        },
        dataType:'json',
        type:'post',
        success:function(data){
            for(var i=0;i<data.length;i++){
                $('#propertyValContainer').append('<span propertyValId="' + data[i].ID + '" class="label label-default propertyVal" onclick="changeValLabelColor(this)">' + data[i].NAME + '</span>');
            }
        }
    });
}
